﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.controllers.world {
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;

    public class LoadRoomCommand extends Command {

        public var event:InfoEvent;
        public var slotsModel:RoomSlotsModel;
        public var userModel:UserModel;
        private var _data:Object;

        override public function execute():void{
            var _local1:Slot = this.slotsModel.getSlotById(this.event.info.slot);
            _local1.setOccupied();
            dispatch(new UserModelEvent(UserModelEvent.ON_MODEL_CHANGED));
            dispatch(new InfoEvent(EventConstants.CREATE_DUMMY_ROOM, (GameConstants.ROOM_DUMMY + _local1.id), _local1.position.clone()));
            var _local2:Object = {taskType:GameConstants.PLACE_ROOM, itemType:this.event.info.room_type, onResult:this.onResult};
            var _local3:RequestVO = new RequestVO("create_room", this.event.info);
            var _local4:RequestVO = new RequestVO(ProtocolConstants.LOAD_ROOM, {id:this.event.info.room_type.toLowerCase()}, true);
            log(this.event.info.elements);
            var _local5:Array = [_local3, _local4];
            var _local6:TaskRequestEvent = new TaskRequestEvent(EventConstants.SEND_TASK_REQUEST, _local5, _local2);
            dispatch(_local6);
            dispatch(new InfoEvent(EventConstants.PLAY_EFFECT_SOUND, GameConstants.SOUND_BUILD_ROOM));
        }
        private function onAssetResult(_arg1:ResultVO):void{
        }
        private function onResult(_arg1:ResultVO):void{
            if (_arg1.success){
                this._data = _arg1.data.create_room;
                this.loadElements();
            };
        }
        private function loadElements():void{
            var _local2:Object;
            var _local1:RequestEvent = new RequestEvent(EventConstants.SEND_REQUEST, this.roomElementsLoaded, GameConstants.CUSTOMIZE_ROOM_ASSET);
            for each (_local2 in this._data.room.elements) {
                log(("load room asset" + _local2.element_type));
                _local1.addAssetRequest(ProtocolConstants.LOAD_ROOM_ASSET, _local2.element_type);
            };
            dispatch(_local1);
        }
        private function roomElementsLoaded(_arg1:ResultVO):void{
            var _local2:Array;
            if (_arg1.success){
                dispatch(new InfoEvent(EventConstants.CREATE_ROOM, null, this._data));
                dispatch(new InfoEvent(EventConstants.UPDATE_USER_DATA, null, this._data));
                _local2 = this._data.room.pets;
                if (_local2.length){
                    dispatch(new InfoEvent(EventConstants.CREATE_NEW_DOCTOR, null, _local2[0]));
                };
                dispatch(new InfoEvent(EventConstants.CLOSE_SHOP, null));
            };
        }

    }
}//package net.wooga.pets.controllers.world 
